<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6
 * Time: 17:19
 */

namespace App\Game\Traits;


use App\Models\QuotaHandicap;
use App\Models\Def\MainGamePlat;

trait SunBetTrait
{
    /**
     * gameapi
     * 2018/12/6 22:50
     * Administrator
     * signature
     * 获取签名
     * @return string
     */
    public function signature()
    {
        $xSgsDate = $this->xSgsDate;
        $stringToSign = $this->clientSecret . $xSgsDate;
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->clientSecret, true));
        return $signature;
    }

    /**
     * gameapi
     * 2019/1/14 18:34
     * Administrator
     * token
     * token
     * @param $accountUserName
     * @param $request
     * @param int $platformType
     * @return null
     */
    private function token($accountUserName, $request,$platformType=1)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = QuotaHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }

        $json = [
            'ipaddress' => $request->getClientIp(),
            'username' => $accountUserName,
            'userid' => $accountUserName,
            'lang' => 'zh-CN',
            'cur' => 'RMB',
            'betlimitid' => $oddType->code ?? '3',
            'platformtype' => $platformType,
            "istestplayer" => false
        ];
        $url = $this->gameUrl . self::API_LOGIN;
        $output = $this->sunBetRequest('POST', [], $url, $this->header, $json);
        $output = json_decode($output, true);
        if (array_key_exists('authtoken', $output)) {
            $token = $output['authtoken'];
        } else {
            $token = null;
        }
        return $token;
    }

    /**
     * gameapi
     * 2018/12/7 22:29
     * Administrator
     * sunBetRequest
     * sunbet请求
     * @param string $method
     * @param $params
     * @param $url
     * @param $headers
     * @param null $json
     * @return mixed
     */
    public function sunBetRequest($method = 'POST', $params, $url, $headers, $json = null)
    {
        $signature = $this->signature();
        $headers = array_merge($headers, ['Authorization' => 'SGS ' . $this->clientId . ':' . $signature, 'X-Sgs-Date' => $this->xSgsDate]);
        $result = $this->request($method, $url, $params, $headers, ['json' => $json]);
        return $result;
    }

    /**
     * gameapi
     * 2018/12/7 22:29
     * Administrator
     * dataFormat
     * 记录数据格式化
     * @param $str
     * @return array
     */
    public function dataFormat($str)
    {
        $data = [];
        $rows = array_filter(explode("\r\n", $str));
        $fieldStr = $rows[0];
        unset($rows[0]);
        $rows = array_values($rows);
        $fieldArray = array_filter(explode(',', $fieldStr));
        foreach ($rows as $row) {
            $values = array_filter(explode(',', $row));
            $data[] = array_combine($fieldArray, $values);
        }

        return $data;
    }
}